<template>
  <div>
    <button @click="isShow = true">打开弹窗</button>
    <teleport to="body">
      <div class="dialog-mask" v-if="isShow">
        <div class="dialog">
          <div>我是组件弹窗</div>
          <div>
            <button @click="isShow = false">关闭弹窗</button>
          </div>
        </div>
      </div>
    </teleport>
  </div>
</template>

<script>
import { setup, ref, Teleport } from "vue";
export default {
  name: "demo-teleport",
  components: { Teleport },
  setup() {
    let isShow = ref(false);
    return {
      isShow,
    };
  },
};
</script>

<style scoped>
.dialog-mask {
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 9999;
  background-color: rgba(0, 0, 0, 0.5);
}
.dialog {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  padding: 30px;
  border: 1px solid red;
}
</style>